<template>
  <div class="login-container flex justify-center align-center">
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">会员管理中心</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          show-password
        >
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin(loginFormRef)"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>

    <!--  底部  -->
    <div class="login-footer text-center">
      <span>Copyright © 2023-2023 legendary.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { RouteLocationNormalizedLoaded } from "vue-router";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { login, getLoginState } from "@/api/user";
import { setToken } from "@/utils/auth";
import useUser from "@/store/useUser";
const userStore = useUser();

const loginFormRef = ref<FormInstance>();
const loading = ref(false);
const loginForm = reactive({
  username: "",
  password: "",
});
const loginRules = reactive<FormRules>({
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
});

const router = useRouter();
const route = useRoute();
const redirect = computed(() => (route.query.redirect as string) || "/");
async function handleLogin(formEl: FormInstance | undefined) {
  try {
    if (!formEl) return;
    await formEl.validate();
    loading.value = true;
    await userStore.login(loginForm);
    login({ username: loginForm.username.trim(), password: loginForm.password })
      .then(async (res) => {
        if (!res) {
          loading.value = false;
          // ElMessage.error('账号或密码错误！')
        } else {
          loading.value = false;
          if ((res as any).data.hasOwnProperty("code")) {
            ElMessage.error("登陆失败！");
          } else {
            let isToken = await getLoginState();
            if (isToken.data.hasOwnProperty("data")) {
              setToken("1");
            }
            await router.push({
              path: redirect.value,
              query: getOtherQuery(route),
            });
            ElMessage.success("登录成功");

          }
        }
      })
      .catch((err) => {
        loading.value = false;
      });
  } catch (error) {
    loading.value = false;
  }
}

interface RouteQueryType {
  redirect?: string;
  [key: string]: any;
}
function getOtherQuery(route: RouteLocationNormalizedLoaded): RouteQueryType {
  const tmp: RouteQueryType = {};
  for (const key in route.query) {
    if (key !== "redirect") tmp[key] = route.query[key];
  }
  return tmp;
}
</script>

<style lang="scss" scoped>
.login-container {
  position: relative;
  width: 100%;
  height: 100%;
  background-size: cover;

  .login-form {
    position: absolute;
    z-index: 2023;
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;

    .title {
      margin: 0px auto 30px auto;
      text-align: center;
      color: #707070;
      font-weight: 500;
    }
  }

  .login-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 40px;
    line-height: 40px;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
}
</style>
